using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.IO;

namespace ParseSplitTextFile
{
    class ParseTF
    {
        //private string fKeyFile = @"D:\DATA\UsercheeOnD\myVS2005\CStestBed\ParseSplitTextFile\book_names.txt";
        //private string fContentFile = @"D:\DATA\UsercheeOnD\myVS2005\CStestBed\ParseSplitTextFile\kjv_apocrypha_utf8.txt";
        private string fKeyFile = @"H:\myCsharp\CSharpTestBed\ParseSplitTextFile\book_names.txt";
        //private string fContentFile = @"H:\myCsharp\CSharpTestBed\ParseSplitTextFile\kjv_apocrypha_utf8.txt";
        //private string fContentFile = @"H:\myCsharp\CSharpTestBed\ParseSplitTextFile\asv_utf8.txt";
        private string fContentFile = @"H:\myCsharp\CSharpTestBed\ParseSplitTextFile\basic_english_utf8.txt";

        private int iKeyIndex = 1;   // column number for code in fKeyFile
        private int iValIndex = 2;   // column number for Bible Book in fKeyFile

        private Hashtable myHash;
        private char[] sep = { '\t' };

        static void Main(string[] args)
        {
            ParseTF aProg = new ParseTF();
            aProg.myHash = new Hashtable();

            // Reading First Mapping File
            try
            {
                // Create an instance of StreamReader to read from a file.
                // The using statement also closes the StreamReader.
                using (StreamReader sr = new StreamReader(aProg.fKeyFile))
                {
                    String line;
                    // Read and display lines from the file until the end of 
                    // the file is reached.
                    while ((line = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(line);
                        string[] words = line.Split();
                        aProg.myHash.Add(words[aProg.iKeyIndex - 1], line.Substring(4)); 
                        //foreach (string word in words)
                        //{
                        //    Console.WriteLine(word);
                        //}
                    }
                }
            }
            catch (Exception e)
            {
                // Let the user know what went wrong.
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }


            // Reading the Content File
            try
            {
                // Create an instance of StreamReader to read from a file.
                // The using statement also closes the StreamReader.
                using (StreamReader sr = new StreamReader(aProg.fContentFile))
                {
                    String line;
                    // Read and display lines from the file until the end of 
                    // the file is reached.

                    StreamWriter sw = null;
                    string stmp1 = "";
                    string stmp2 = "";

                    while ((line = sr.ReadLine()) != null)
                    {

                        Console.WriteLine(line);
                        // Finding book and chapter
                        string sBookChap = line.Substring(0, 15);
                        string[] words = sBookChap.Split();
                        string sBook = words[0];
                        string sChap = words[1];
                        string sBookName = (string) aProg.myHash[sBook];
                        
                        string sText = line.Substring(12);

                        string[] content = line.Split(aProg.sep);   // split by tab '\t'
                        string sVerseNum = content[2];
                        string sVerseText = content[5];

                        if (stmp1.Equals(sBook) && stmp2.Equals(sChap))
                        {
                            // Add some text to the file.
                            if (sw != null)
                            {
                                sw.WriteLine(sVerseNum + " " + sVerseText);
                            }
                        }
                        else
                        {
                            // If not at the very beginning 
                            if (!stmp1.Equals("")) { sw.Close(); }
                            sw = new StreamWriter(sBookName+words[1]+".txt");
                            stmp1 = sBook;
                            stmp2 = sChap;
                            // Add some text to the file.
                            sw.WriteLine(sVerseNum + " " + sVerseText);
                        }

                        // Create an instance of StreamWriter to write text to a file.
                        // The using statement also closes the StreamWriter.
                        //using (StreamWriter sw = new StreamWriter(sBookName+words[1]+".txt"))
                        //{
                        //    // Add some text to the file.
                        //    sw.WriteLine(sVerseNum + " " + sVerseText);
                        //}

                    }
                }
            }
            catch (Exception e)
            {
                // Let the user know what went wrong.
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }


            Console.ReadLine();
        }
    }
}
